Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Public Class QtyDetailForm
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ltrMessage As System.Web.UI.WebControls.Literal
    Protected WithEvents ddlCustomer As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents ddlProduct As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Requiredfieldvalidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtQty As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ltrReservedDate As System.Web.UI.WebControls.Literal
    Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents txtNumberOfDay As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator4 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents Comparevalidator2 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents RangeValidator1 As System.Web.UI.WebControls.RangeValidator

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Protected mainObj As CustomerProduct = Nothing

    Protected Property ReferrerUrl() As String
        Get
            Return Convert.ToString(ViewState("ReferrerUrl"))
        End Get
        Set(ByVal Value As String)
            ViewState("ReferrerUrl") = Value
        End Set
    End Property

    Protected ReadOnly Property IsAddNew() As Boolean
        Get
            Return RequestContext.UserID = -1
        End Get
    End Property

    Private Function BuildObject() As Boolean
        If IsAddNew Then
            mainObj = New CustomerProduct
        Else
            mainObj = CustomerProductService.GetCustomerProduct(RequestContext.UserID, RequestContext.ProductCode)
            If mainObj Is Nothing Then
                Utilities.ShowMessagePage("Failed to load! CustomerProduct may be deleted or not found")
                Return False
            End If
        End If

        Return True
    End Function

    Private Sub UpdateData(ByVal bSave As Boolean)
        If (bSave) Then
            mainObj.UserID = Convert.ToInt32(ddlCustomer.SelectedItem.Value)
            mainObj.ProductCode = ddlProduct.SelectedItem.Value
            Try
                If txtQty.Text.Trim().Length > 0 Then mainObj.ReservedQty = Convert.ToInt32(txtQty.Text.Trim())
            Catch ex As Exception
            End Try

            mainObj.ReservedDate = DateTime.Now
            mainObj.NumberOfDay = Convert.ToInt32(txtNumberOfDay.Text)
        Else
            ddlCustomer.SelectedIndex = ddlCustomer.Items.IndexOf(ddlCustomer.Items.FindByValue(mainObj.UserID.ToString()))
            ddlProduct.SelectedIndex = ddlProduct.Items.IndexOf(ddlProduct.Items.FindByValue(mainObj.ProductCode))
            If (mainObj.ReservedQty <> Nulls.NullInteger) Then
                txtQty.Text = mainObj.ReservedQty.ToString()
            End If
            If (mainObj.ReservedDate <> Nulls.NullDate) Then
                ltrReservedDate.Text = mainObj.ReservedDate.ToShortDateString()
            End If

            txtNumberOfDay.Text = mainObj.NumberOfDay.ToString()
        End If
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Security.IsCustomer Then
            Throw New Exception("You are not allowed here")
        End If

        If Not IsPostBack Then
            ReferrerUrl = Utilities.GetReferrerUrl("CustomerReservedList.aspx")

            ddlCustomer.DataSource = USERService.GetCustomerList()
            ddlCustomer.DataTextField = USERColumns.LOGIN_NAME.ToString()
            ddlCustomer.DataValueField = USERColumns.US_ID_PK.ToString()
            ddlCustomer.DataBind()

            ddlCustomer.Items.Insert(0, New ListItem("[select an available customer", "-1"))

            ddlProduct.DataSource = BulkOrderService.GetProductList()
            ddlProduct.DataTextField = BulkOrderColumns.ProductCode.ToString()
            ddlProduct.DataValueField = BulkOrderColumns.ProductCode.ToString()
            ddlProduct.DataBind()
            ddlProduct.Items.Insert(0, New ListItem("[select a product]", "-1"))

            If IsAddNew Then
                btnDelete.Visible = False
                ltrReservedDate.Text = DateTime.Now.ToShortDateString()
            Else
                If (BuildObject()) Then
                    UpdateData(False)
                End If

                btnDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                btnDelete.Visible = True
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If (Page.IsValid) Then
            If (BuildObject()) Then

                UpdateData(True)
                If IsAddNew Then
                    CustomerProductService.CreateCustomerProduct(mainObj)
                Else
                    CustomerProductService.UpdateCustomerProduct(mainObj)
                End If

                Response.Redirect(ReferrerUrl)
            End If
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        CustomerProductService.DeleteCustomerProduct(RequestContext.UserID, RequestContext.ProductCode)
        Response.Redirect(ReferrerUrl)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(ReferrerUrl)
    End Sub
End Class
